#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: pages.py
@contact: zwb15083976291@163.com
@time: 2021/12/06
"""
from django.utils.decorators import method_decorator

from admin.serializers.pages import PagesSerializer
from common.models import BlogPages
from utils.filters.views_filters import SearchPagesFilter
from utils.responses import CommonResponse, CommonAPIResponseStatus
from utils.user_authentication import allow_admin_user
from utils.views import BaseGenericAPIView


@method_decorator(allow_admin_user, name='dispatch')
class ManagementPagesView(BaseGenericAPIView):
    serializer_class = PagesSerializer
    diff_methods_request_params = dict(get=dict(optional=["page_label"]),
                                       put=dict(required=['id', 'background_image_url']))

    filter_backends = [SearchPagesFilter]

    def get(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)

        return CommonResponse(data=self.serializer_class(self.filter_queryset(BlogPages.objects.all()), many=True).data)

    def put(self, request):
        ret, message = self.validate_request_params()
        if not ret:
            return CommonResponse(CommonAPIResponseStatus.BAD_REQUEST, message=message)
        self.serializer.select_field_update_obj()
        return CommonResponse(data=self.serializer.data)
